<?php
/**
 * Clase para mostrar contenido en el panel de administración de CMS
 *
 * @author Renzo Zamora
 **/
class requires{
	private $pages = array(
		'default' => "dashboard.php",
		'content' => "content.php",
		
		'structure/types' => 'content_types.php',
		'' => '',
		'' => '',
		'' => '',
	);
	private $official_pages = array(
		'default', 
		'content', 
		'structure/types'
	);
	private $error_page = 'error.php';

	public function __construct(){
		if( has_filter('admin_pages') )
			$this->pages = apply_filters('admin_pages', $this->pages);

		if( has_filter('official_pages') )
		$this->official_pages = apply_filters('official_pages', $this->official_pages);

		foreach( $this->official_pages as $current ){
			$this->pages[$current] = ADMINPATH . $this->pages[$current];
		}
		$this->error_page = ADMINPATH . $this->error_page;
	}

	public function add_page($key, $value){
		if( isset($this->pages[$key]) )
			return false;
		
		$this->pages[$key] = $value;
		return true;
	}
	public function edit_page($key, $value){
		if( !isset($this->pages[$key]) )
			return false;
		
		$this->pages[$key] = $value;
		return true;	
	}
	public function remove_page($key){
		if( !isset($this->pages[$key]) )
			return false;
		
		unset($this->pages[$key]);

		$index = array_search($key, $this->official_pages);
		if( !$index || !is_null($index) ){
			unset($this->official_pages[$index]);
			$this->official_pages = array_values($this->official_pages);
		}
		return true;
	}
	public function add_official_page($key, $value=NULL){
		if( in_array($key, $this->official_pages) )
			return false;

		if( !is_null($key) && !isset($this->pages[$key]) ){
			$this->pages[$key] = $value;
		}

		array_push($this->official_pages, $key);

		return true;
	}
	public function remove_official_page($key){
		if( !in_array($key, $this->official_pages) )
			return false;

		$index = array_search($key, $this->official_pages);
		if( !$index || !is_null($index) ){
			unset($this->official_pages[$index]);
			$this->official_pages = array_values($this->official_pages);
		}
		return true;
	}

	public function get(){

		if( isset($_GET['page']) ){
			$page = $_GET['page'];
			do_action( 'admin_pages' );

			if( isset($this->pages[$page]) ){
				require $this->pages[$page];
			} else {
				require $this->error_page;
			}
		} else {
			require $this->pages['default'];
		}
	}

}
?>